package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Userlike;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Xiao
 * @version 1.0
 * @description
 * @date 2021/10/31
 */
@DubboService
public class UserLikeImpl implements UserLikeApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Userlike> getLikes(Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId);
        return mongoTemplate.find(Query.query(criteria), Userlike.class);
    }

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, Boolean isLike) {
        try {
            Criteria criteria = Criteria
                    .where("userId").is(userId)
                    .and("likeUserId").is(likeUserId);
            Query query = Query.query(criteria);
            boolean exists = mongoTemplate.exists(query, Userlike.class);
            if (exists) {
                Update update = Update.update("isLike", isLike)
                        .set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, Userlike.class);
            } else {
                Userlike userLike = new Userlike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setIsLike(isLike);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                mongoTemplate.save(userLike);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Map<Long, Userlike> selectByIds(Long userId, List<Long> userIds) {
        Criteria criteria = Criteria
                .where("userId").is(userId)
                .and("likeUserId").in(userIds);
        Query query = Query.query(criteria);
        List<Userlike> userlikes = mongoTemplate.find(query, Userlike.class);
        //使用CollUtil.fieldValueMap()构造Map对象
//        Map<Long, Userlike> map = CollUtil.fieldValueMap(userlikes, "likeUserId");
        //使用stream().collect()构造Map对象
        return userlikes.stream().collect(Collectors.toMap(Userlike::getLikeUserId, Function.identity()));
    }

    @Override
    public List<Userlike> queryLikesMe(Long userId) {
        Criteria criteria = Criteria
                .where("likeUserId").is(userId)
                .and("isLike").is(true);
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("updated")));
        return mongoTemplate.find(query, Userlike.class);
    }

    @Override
    public List<Userlike> queryfanCount(Long userId) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        return mongoTemplate.find(query, Userlike.class);
    }
}
